﻿//-------------------------------------------------------------------------------
// <copyright file="FBConnection.cs" company="TaskyMedia LLC">
//     Copyright (c) TaskyMedia LLC.
// </copyright>
//-------------------------------------------------------------------------------

/*
 * Copyright (c) 2010, TaskyMedia LLC
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *   Redistributions of source code must retain the above copyright notice, this 
 *     list of conditions and the following disclaimer.
 *   Redistributions in binary form must reproduce the above copyright notice, this
 *     list of conditions and the following disclaimer in the documentation and/or
 *     other materials provided with the distribution.
 *   Neither the name of TaskyMedia LLC nor the names of its contributors may be used
 *   to endorse or promote products derived from this software without specific prior
 *   written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE. 
 */

namespace FBGraphApi
{
    using System;
    using System.Collections.Generic;
    using Newtonsoft.Json.Linq;

    /// <summary>
    /// A relationship between two object types in the Facebook social graph.
    /// </summary>
    /// <typeparam name="T">The type of object in the connection; must derive from <see cref="FBObject" />.</typeparam>
    public class FBConnection<T> : FBObject, IEnumerable<T>
        where T : FBObject, new()
    {
        /// <summary>
        /// Retains a list of connections.
        /// </summary>
        private List<T> _connectionData;

        /// <summary>
        /// Initializes a new instance of the <see cref="FBConnection&lt;T&gt;"/> class.
        /// </summary>
        public FBConnection()
        {
            this._connectionData = new List<T>();
        }

        /// <summary>
        /// Gets the number of objects in the connection.
        /// </summary>
        /// <value>The number of objects in the connection.</value>
        public int Count
        {
            get
            {
                return this._connectionData.Count;
            }
        }

        /// <summary>
        /// Gets the object at the specified index.
        /// </summary>
        /// <param name="index">The index at which to retrieve the object.</param>
        /// <value>The object at the specified index.</value>
        public T this[int index]
        {
            get
            {
                return this._connectionData[index];
            }
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            return this._connectionData.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this._connectionData.GetEnumerator();
        }

        /// <summary>
        /// Fills the connections with the JSON result.
        /// </summary>
        /// <param name="jsonData">The JSON data.</param>
        internal override void FillObject(string jsonData)
        {
            JObject jsonObject = JObject.Parse(jsonData);
            JToken jsonArry = jsonObject["data"];
            foreach (JToken token in jsonArry)
            {
                string data = token.ToString();
                T val = new T();
                val.FillObject(data);
                this._connectionData.Add(val);
            }
        }
    }
}
